package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.BeforeDyeMapper;
import com.zhijian.medical.entity.po.BeforeDye;
import com.zhijian.medical.entity.vo.request.BeforeDyeInsideSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.BeforeDyeSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.BeforeDyeSearchVo;
import com.zhijian.medical.entity.vo.response.BeforeDyeListVo;
import com.zhijian.medical.entity.vo.response.BeforeDyeVo;
import com.zhijian.medical.enums.DyeEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.BeforeDyeService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.NumUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 脱水前的样本放置容器表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-11-17 15:24:59
 */
@Slf4j
@Service
public class BeforeDyeServiceImpl implements BeforeDyeService {

    @Autowired
    private BeforeDyeMapper mapper;

    @Override
    public BeforeDye selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public BeforeDyeVo selectVoById(Long id) {
        BeforeDyeVo beforeDyeVo = mapper.selectVoById(id);
        if (Objects.isNull(beforeDyeVo)) {
            return null;
        }
        String sampleIds = beforeDyeVo.getSampleIds();
        if (StringUtils.isBlank(sampleIds)) {
            return beforeDyeVo;
        }
        //循环样本的分子病理号，查出所有的样本VO
        beforeDyeVo.setSampleList(Arrays.stream(sampleIds.split(",")).collect(Collectors.toList()));
        return beforeDyeVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(BeforeDye beforeDye) {
        Date now = new Date();
        beforeDye.setCreateTime(now);
        beforeDye.setUpdateTime(now);
        return mapper.insert(beforeDye) > 0 ? beforeDye.getId() : 0L;
    }

    @Override
    public boolean update(BeforeDye beforeDye) {
        Date now = new Date();
        beforeDye.setUpdateTime(now);
        return mapper.updateByPrimaryKey(beforeDye) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(BeforeDye record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(BeforeDyeSearchVo beforeDyeSearchVo) {
        return mapper.count(beforeDyeSearchVo);
    }

    @Override
    public List<BeforeDyeListVo> selectList(BeforeDyeSearchVo beforeDyeSearchVo) {
        return mapper.selectList(beforeDyeSearchVo);
    }

    @Override
    public Page<BeforeDyeListVo> selectPage(BeforeDyeSearchVo beforeDyeSearchVo) {
        //先直接查总数
        int count = count(beforeDyeSearchVo);
        //分页
        Page<BeforeDyeListVo> page = new Page<>(true, count, beforeDyeSearchVo.getPageNum(), beforeDyeSearchVo.getPageSize());
        beforeDyeSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<BeforeDyeListVo> responseVos = this.selectList(beforeDyeSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<BeforeDye> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public boolean save(BeforeDyeSaveOrUpdateVo beforeDyeSaveOrUpdateVo) {
        AssertUtil.check(beforeDyeSaveOrUpdateVo.getBeginSampleNumber() >= beforeDyeSaveOrUpdateVo.getEndSampleNumber(), "开始样本号要小于结束样本号");
        //找出这俩样本，中间的所有样本
        Integer[] range = NumUtil.range(beforeDyeSaveOrUpdateVo.getBeginSampleNumber(), beforeDyeSaveOrUpdateVo.getEndSampleNumber(), 1);

        BeforeDye beforeDye = new BeforeDye();
        beforeDye.setContainerNumber(beforeDyeSaveOrUpdateVo.getContainerNumber());
        beforeDye.setSampleIds(String.join(",", Arrays.stream(range).map(String::valueOf).collect(Collectors.toList())));
        //默认待脱水
        beforeDye.setStatus(DyeEnum.WAIT_DYE.getCode().byteValue());
        beforeDye.setOperateUserId(CurrentUserHolder.getUser().getId());
        String processNumber = String.format("Dye%s", DateUtil.format(new Date(), new SimpleDateFormat("yyyyMMddHHmmss")));
        beforeDye.setProcessNumber(processNumber);
        return this.insert(beforeDye) != 0;
    }

    @Override
    public boolean insideSave(BeforeDyeInsideSaveOrUpdateVo beforeDehydrationInsideSaveOrUpdateVo) {
        List<String> sampleMoleculePathologyNumbers = beforeDehydrationInsideSaveOrUpdateVo.getSampleMoleculePathologyNumbers();
        BeforeDye beforeDye = this.selectById(beforeDehydrationInsideSaveOrUpdateVo.getId());
        return mapper.updateByPrimaryKeySelective(BeforeDye.builder()
                .id(beforeDye.getId())
                .sampleIds(String.join(",", sampleMoleculePathologyNumbers))
                .build()) != 0;
    }

}